<?php
/**
 * Created by PhpStorm.
 * User: silenceper
 * Date: 2016/11/24
 * Time: 下午1:55
 */

namespace mybackend\controllers;


use common\entities\myhtsq\ActionCode;
use common\services\common\MyscrmOrganizationService;
use common\services\myhtsq\ProjectService;

class ProjectController extends BaseController
{

    /* @var $_projectContactService ProjectService */
    private $_projectService;

    public function __construct($id, $module, ProjectService $projectService, $config = [])
    {
        $this->_projectService = $projectService;
        parent::__construct($id, $module, $config);
    }

    protected function roleAuthRules()
    {
        return [
            'actions' => ['GetProject', 'GetContact', 'UpdateContact'],
            'rules' => [
                [
                    'allow' => true,
                    'actionCodeMaps' => [
                        'GetProject' => ActionCode::EXP_AUTH_VIEW,
                        'GetContact' => ActionCode::EXP_AUTH_VIEW,
                        'GetContactChangeLog' => ActionCode::EXP_AUTH_VIEW,
                        'UpdateContact' => ActionCode::EXP_JFXX_MOD,
                    ]
                ]
            ]
        ];
    }

    /**
     * 根据租户获取项目
     * @param $orgId
     */
    public function actionGetProject($orgId)
    {
        /* @var $myscrmOrganizationService MyscrmOrganizationService */
        $myscrmOrganizationService = \Yii::createObject(MyscrmOrganizationService::className());
        $data = $myscrmOrganizationService->getAllAuthProject("", "", "", "", $orgId);
        $result = [];
        foreach ($data as $value) {
            $projId = $value["ProjId"];
            if (empty($projId)) {
                continue;
            }
            if (!isset($result["$projId"])) {
                $item = [
                    "ProjId" => $value["ProjId"],
                    "ProjName" => $value["ProjName"],
                ];
                $result[$projId] = $item;
            }
        }

        $this->exportJson(array_values($result));
    }

    /**
     * 查看交付信息
     * @param $orgId
     * @param $projectId
     */
    public function actionGetContact($orgId, $projectId)
    {
        $data = $this->_projectService->getDeliverInfo($orgId, $projectId);
        $this->exportJson($data, $this->_projectService->getErrCode(), $this->_projectService->getErrMsg());
    }

    /**
     * 更新交付信息
     */
    public function actionUpdateContact()
    {
        $orgId = \Yii::$app->request->post("orgId");
        $projectId = \Yii::$app->request->post("projectId");
        $customManageGuid = \Yii::$app->request->post("custom_manage_guid");
        $deliverGuid = \Yii::$app->request->post("deliver_guid");
        $isBatch = \Yii::$app->request->post("is_batch", 0);

        if (empty($orgId) || empty($projectId) || empty($customManageGuid || empty($deliverGuid))) {
            $this->exportJson([], -1, "参数必填");
        }
        if (!is_array($projectId) || !is_array($customManageGuid) || !is_array($deliverGuid)) {
            $this->exportJson([], -1, "参数格式错误");
        }
        $data = [
            'orgId' => $orgId,
            'projectId' => $projectId,
            'custom_manage_guid' => $customManageGuid,
            'deliver_guid' => $deliverGuid,
            'is_batch' => $isBatch,
        ];
        $this->_projectService->updateContact($data);
        $this->exportJson([], $this->_projectService->getErrCode(), $this->_projectService->getErrMsg());
    }

    /**
     * 获取交付人修改记录
     * @param $orgId
     * @param $projectId
     */
    public function actionGetContactChangeLog($orgId, $projectId)
    {
        $data = $this->_projectService->getContactChangeLog($orgId, $projectId);
        $this->exportJson($data, $this->_projectService->getErrCode(), $this->_projectService->getErrMsg());
    }
}